package ai.people.netmon.telegram.service.impl;

//import ai.people.netmon.feign.telegram.ElasticsearchClient;

import ai.people.core.feign.api.elastic.ElasticsearchClient;
import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.domain.searchmsg.request.MessageRequest;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChatMessage;
import ai.people.netmon.framework.model.request.PageKeyWordsRequest;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.telegram.service.MessagesService;
import ai.people.netmon.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息服务impl
 *
 * @author yuanqinglong
 * @date 2022/1/10 17:35
 */
@Service
public class MessagesServiceImpl implements MessagesService {


    @Autowired
    private ElasticsearchClient elasticsearchClient;


    /**
     * 分页查询信息列表
     *
     * @param pageKeyWordsRequest 页面关键字请求
     * @return {@link PageResponse}<{@link ChatMessage}>
     */
    @Override
    public PageResponse<ChatMessage> businessChannelsList(PageKeyWordsRequest pageKeyWordsRequest) {
        MessageRequest messageRequest = new MessageRequest();
        messageRequest.setLimit(pageKeyWordsRequest.getPageSize());
        messageRequest.setOffset(pageKeyWordsRequest.getPageNo());
        messageRequest.setSearchKeywords(pageKeyWordsRequest.getSearchKeywords());
        messageRequest.setSource(AnalysisSourceEnum.telegram.name());
        return this.globalInfoSearch(messageRequest);
    }


    /**
     * 全局信息搜索
     *
     * @param messageRequest 消息请求
     * @return {@link PageResponse}<{@link ChatMessage}>
     */
    @Override
    public PageResponse<ChatMessage> globalInfoSearch(MessageRequest messageRequest) {
        Result result = elasticsearchClient.esRetrieve(messageRequest);
        if (!result.getStatus()) {
            return PageResponse.builderEmpty();
        }
        PageResponse<?> pageResponse = JSONUtils.convertObject(result.getData(), PageResponse.class);
        List<ChatMessage> chatMessageList = pageResponse.getList().stream().map(o -> JSONUtils.convertObject(o, ChatMessage.class)).collect(Collectors.toList());
        return PageResponse.builder(chatMessageList, pageResponse.getTotal());
    }
}
